//
//  RoyaDialView.m
//  Test
//
//  Created by royasoft on 12-12-5.
//  Copyright (c) 2012年 royasoft. All rights reserved.
//

#import "RoyaDialView.h"
#import "RoyaDialViewDelegate.h"

#define CONFIGURE_BUTTON(BTN,X,Y,TITLE,TAG) {\
                                    self.BTN = [UIButton buttonWithType:UIButtonTypeCustom];\
                                    [self.BTN setFrame:CGRectMake(X, Y, widthOfButton, heightOfButton)];\
                                    [self.BTN setTintColor:[UIColor whiteColor]];\
                                    [self.BTN setBackgroundColor:[UIColor darkGrayColor]];\
                                    [self.BTN setTag:TAG];\
                                    [self.BTN addTarget:self action:@selector(onKeyPressed:) \
                                                forControlEvents:UIControlEventTouchUpInside];\
                                    [self addSubview:self.BTN];\
                                   }

#define CONFIGURE_BUTTON_DIAL(BTN,X,Y,TITLE,TAG) {\
                                    self.BTN = [UIButton buttonWithType:UIButtonTypeCustom];\
                                    [self.BTN setFrame:CGRectMake(X, Y, widthOfButton, heightOfButton*3)];\
                                    [self.BTN setTintColor:[UIColor whiteColor]];\
                                    [self.BTN setBackgroundColor:[UIColor darkGrayColor]];\
                                    [self.BTN setTag:TAG];\
                                    [self.BTN addTarget:self action:@selector(onKeyPressed:) \
                                    forControlEvents:UIControlEventTouchUpInside];\
                                    [self addSubview:self.BTN];\
}


#define Key_high 45
#define Keyboard_toolBar_high 44
#define TAG_KEY_DIAL            111
#define TAG_KEY_Xin             222
#define TAG_KEY_Jin             333
#define TAG_KEY_UNDO            444
#define TAG_KEY_Add             555

//private
@interface RoyaDialView(private)

-(void)setLayOn:(BOOL) isLayOn;

-(void)handleCall:(NSString *) phoneNum;

@end

@implementation RoyaDialView(private)

#pragma mark 响应 显示 隐藏 键盘
-(void)setLayOn:(BOOL)isLayOn
{
    CGFloat adjustOffset = self.frame.size.height - Keyboard_toolBar_high;//Eonman 改
    
    if (isLayOn == YES) {
        adjustOffset *= -1;
    }
   
    CGPoint center = self.center;
    center.y += adjustOffset;
    self.center = center;
}

#pragma mark 打电话按钮方法
-(void)handleCall:(NSString *) phoneNum
{
    if(phoneNum.length){
        if ([self.delegate  respondsToSelector:@selector(onDialView:makePhoneCall:)]) {
            [self.delegate onDialView:self makePhoneCall:phoneNum];
        }else{
                NSString *num = [[NSString alloc] initWithFormat:@"telprompt://%@",phoneNum];
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:num]];
                [num release];
              }
    }
}

@end

//public
@implementation RoyaDialView
{
    UILabel *txtNumber;
}

-(id)init:(UILabel *)txtNumber_
{
    txtNumber = txtNumber_;
    
    CGRect frame = [[UIScreen mainScreen]applicationFrame];
    frame.size.height = Key_high * 4 + Keyboard_toolBar_high;//Eonman 改
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        UIImageView *keyboard_bg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, Keyboard_toolBar_high)];
        keyboard_bg.image = [UIImage imageNamed:@"keybord_input_bg.png"];
        [self addSubview:keyboard_bg];
        
        CGFloat heightOfView = Keyboard_toolBar_high;
        CGFloat widthOfView = 60.0;
        self.btnOffOn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.btnOffOn setFrame:CGRectMake(0, 5, widthOfView, Keyboard_toolBar_high - 10)];
        [self.btnOffOn setImage:[UIImage imageNamed:@"key_up_on.png"] forState:UIControlStateNormal];
        [self.btnOffOn setImage:[UIImage imageNamed:@"key_up_off.png"] forState:UIControlStateHighlighted];
        [self.btnOffOn addTarget:self action:@selector(onButtonOnOffPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.btnOffOn];
        
        UILabel *keyboard_label = [[UILabel alloc]initWithFrame:CGRectMake(widthOfView, 5, ScreenWidth - widthOfView * 2, Keyboard_toolBar_high-10)];
        keyboard_label.textAlignment = NSTextAlignmentCenter;
        keyboard_label.textColor = [UIColor whiteColor];
        keyboard_label.text = @"固化请加拨区号";
        [self addSubview:keyboard_label];
        
        UIButton *delButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [delButton setFrame:CGRectMake(ScreenWidth - widthOfView, 5, widthOfView, Keyboard_toolBar_high - 10)];
        [delButton setImage:[UIImage imageNamed:@"key_del_off.png"] forState:UIControlStateNormal];
        [delButton setImage:[UIImage imageNamed:@"key_del_on.png"] forState:UIControlStateHighlighted];
        [delButton addTarget:self action:@selector(onKeyPressed:) forControlEvents:UIControlEventTouchUpInside];
        delButton.tag = TAG_KEY_UNDO;
        [self addSubview:delButton];
        
        CGFloat heightOfButton = (frame.size.height - Keyboard_toolBar_high) / 4.0;//Eonman 改
        CGFloat widthOfButton = frame.size.width / 4.0;
        
        //configure the number key
        CONFIGURE_BUTTON(btn1, 0, heightOfView, @"1",1);
        [self.btn1 setTitle:@"1" forState:(UIControlStateNormal)];
        [self.btn1 setBackgroundImage:[UIImage imageNamed:@"key_common_off.png"] forState:UIControlStateNormal];
        [self.btn1 setBackgroundImage:[UIImage imageNamed:@"key_common_on.png"] forState:UIControlStateHighlighted];
        
        CONFIGURE_BUTTON(btn2, widthOfButton , heightOfView, @"2",2);
        [self.btn2 setTitle:@"2" forState:(UIControlStateNormal)];
        [self.btn2 setBackgroundImage:[UIImage imageNamed:@"key_common_off.png"] forState:UIControlStateNormal];
        [self.btn2 setBackgroundImage:[UIImage imageNamed:@"key_common_on.png"] forState:UIControlStateHighlighted];
        
        CONFIGURE_BUTTON(btn3, widthOfButton*2, heightOfView, @"3",3);
        [self.btn3 setTitle:@"3" forState:(UIControlStateNormal)];
        [self.btn3 setBackgroundImage:[UIImage imageNamed:@"key_common_off.png"] forState:UIControlStateNormal];
        [self.btn3 setBackgroundImage:[UIImage imageNamed:@"key_common_on.png"] forState:UIControlStateHighlighted];
        
        CONFIGURE_BUTTON(btn4, 0, heightOfButton + heightOfView, @"4",4);
        [self.btn4 setTitle:@"4" forState:(UIControlStateNormal)];
        [self.btn4 setBackgroundImage:[UIImage imageNamed:@"key_common_off.png"] forState:UIControlStateNormal];
        [self.btn4 setBackgroundImage:[UIImage imageNamed:@"key_common_on.png"] forState:UIControlStateHighlighted];
        
        CONFIGURE_BUTTON(btn5, widthOfButton, heightOfButton + heightOfView, @"5",5);
        [self.btn5 setTitle:@"5" forState:(UIControlStateNormal)];
        [self.btn5 setBackgroundImage:[UIImage imageNamed:@"key_common_off.png"] forState:UIControlStateNormal];
        [self.btn5 setBackgroundImage:[UIImage imageNamed:@"key_common_on.png"] forState:UIControlStateHighlighted];
        
        CONFIGURE_BUTTON(btn6, widthOfButton*2, heightOfButton + heightOfView, @"6",6);
        [self.btn6 setTitle:@"6" forState:(UIControlStateNormal)];
        [self.btn6 setBackgroundImage:[UIImage imageNamed:@"key_common_off.png"] forState:UIControlStateNormal];
        [self.btn6 setBackgroundImage:[UIImage imageNamed:@"key_common_on.png"] forState:UIControlStateHighlighted];
        
        CONFIGURE_BUTTON(btn7, 0, heightOfButton*2 + heightOfView, @"7",7);
        [self.btn7 setTitle:@"7" forState:(UIControlStateNormal)];
        [self.btn7 setBackgroundImage:[UIImage imageNamed:@"key_common_off.png"] forState:UIControlStateNormal];
        [self.btn7 setBackgroundImage:[UIImage imageNamed:@"key_common_on.png"] forState:UIControlStateHighlighted];
        
        CONFIGURE_BUTTON(btn8, widthOfButton, heightOfButton*2 + heightOfView, @"8",8);
        [self.btn8 setTitle:@"8" forState:(UIControlStateNormal)];
        [self.btn8 setBackgroundImage:[UIImage imageNamed:@"key_common_off.png"] forState:UIControlStateNormal];
        [self.btn8 setBackgroundImage:[UIImage imageNamed:@"key_common_on.png"] forState:UIControlStateHighlighted];
        
        CONFIGURE_BUTTON(btn9, widthOfButton*2, heightOfButton*2 + heightOfView, @"9",9);
        [self.btn9 setTitle:@"9" forState:(UIControlStateNormal)];
        [self.btn9 setBackgroundImage:[UIImage imageNamed:@"key_common_off.png"] forState:UIControlStateNormal];
        [self.btn9 setBackgroundImage:[UIImage imageNamed:@"key_common_on.png"] forState:UIControlStateHighlighted];
        
        CONFIGURE_BUTTON(btn_xin,0,heightOfButton*3 + heightOfView, @"*",TAG_KEY_Xin);
        [self.btn_xin setTitle:@"*" forState:(UIControlStateNormal)];
        [self.btn_xin setBackgroundImage:[UIImage imageNamed:@"key_common_off.png"] forState:UIControlStateNormal];
        [self.btn_xin setBackgroundImage:[UIImage imageNamed:@"key_common_on.png"] forState:UIControlStateHighlighted];
        
        
        CONFIGURE_BUTTON(btn0, widthOfButton, heightOfButton*3 + heightOfView, @"0",0);
        [self.btn0 setTitle:@"0" forState:(UIControlStateNormal)];
        [self.btn0 setBackgroundImage:[UIImage imageNamed:@"key_common_off.png"] forState:UIControlStateNormal];
        [self.btn0 setBackgroundImage:[UIImage imageNamed:@"key_common_on.png"] forState:UIControlStateHighlighted];
        
        CONFIGURE_BUTTON(btn_jin, widthOfButton*2, heightOfButton*3 + heightOfView, @"#",TAG_KEY_Jin);
        [self.btn_jin setTitle:@"#" forState:(UIControlStateNormal)];
        [self.btn_jin setBackgroundImage:[UIImage imageNamed:@"key_common_off.png"] forState:UIControlStateNormal];
        [self.btn_jin setBackgroundImage:[UIImage imageNamed:@"key_common_on.png"] forState:UIControlStateHighlighted];
        
        
         //configure the funcion key
        CONFIGURE_BUTTON(btn_add,widthOfButton*3,heightOfView, @"add",TAG_KEY_Add);
        [self.btn_add setBackgroundImage:[UIImage imageNamed:@"key_add_off.png"] forState:UIControlStateNormal];
        [self.btn_add setBackgroundImage:[UIImage imageNamed:@"key_add_on.png"] forState:UIControlStateHighlighted];
        
        CONFIGURE_BUTTON_DIAL(btn_dial,widthOfButton*3,heightOfButton + heightOfView, @"Dial",TAG_KEY_DIAL);
        [self.btn_dial setBackgroundImage:[UIImage imageNamed:@"key_call_off.png"] forState:UIControlStateNormal];
        [self.btn_dial setBackgroundImage:[UIImage imageNamed:@"key_call_on.png"] forState:UIControlStateHighlighted];
        
        self.backgroundColor = [UIColor darkGrayColor];
    }
    return self;

}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
#pragma mark 点击键盘响应方法
-(void)onKeyPressed:(id)sender
{
    NSInteger tag = [sender tag];
    NSString *text = txtNumber.text;
    switch (tag) {
        case TAG_KEY_DIAL:
            [self handleCall:text];
            break;
        case TAG_KEY_Xin:
            txtNumber.text = [NSString stringWithFormat:@"%@*",text];;
            break;
        case TAG_KEY_Jin:
            txtNumber.text = [NSString stringWithFormat:@"%@#",text];;
            break;
       case TAG_KEY_UNDO:
            if (text.length) {
                txtNumber.text = [text stringByReplacingCharactersInRange:NSMakeRange(text.length-1, 1) withString:@""];
            }
            break;
        case TAG_KEY_Add:
            if (text.length) {
                NSLog(@"添加电话 : %@",text);
            }
            break;
       default:
            txtNumber.text = [NSString stringWithFormat:@"%@%ld",text,(long)tag];
            break;
    }
    if ([self.delegate respondsToSelector:@selector(onDialView:dialNumber:withKey:)]) {
        [self.delegate onDialView:self dialNumber:text withKey:tag];
    }
    
}

#pragma mark 键盘显示动画
-(void)onButtonOnOffPressed:(id)sender
{
    UIImage *image = [UIImage imageNamed:@"key_down_off.png"];
    UIImage *image_h = [UIImage imageNamed:@"key_down_on.png"];
    if (mIsLayOn) {
        image = [UIImage imageNamed:@"key_up_on.png"];
        image_h = [UIImage imageNamed:@"key_up_off.png"];
    }
    mIsLayOn ? (mIsLayOn = NO) : (mIsLayOn = YES);
    [self.btnOffOn setImage:image forState:UIControlStateNormal];
    [self.btnOffOn setImage:image_h forState:UIControlStateHighlighted];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    [self setLayOn:mIsLayOn];
    [UIView commitAnimations];
    txtNumber.text = @"";
}

#pragma mark 进入页面时 是否显示键盘
-(void)showInView:(UIView *)view
{
    CGPoint center = self.center;
    center.y = ScreenHeight - 20 + (self.frame.size.height - Keyboard_toolBar_high) / 2;//Eonman 改
    self.center = center;
    [view addSubview:self];
}

-(void)dealloc
{
    [self.btn0 release];
    [self.btn1 release];
    [self.btn2 release];
    [self.btn3 release];
    [self.btn4 release];
    [self.btn5 release];
    [self.btn6 release];
    [self.btn7 release];
    [self.btn8 release];
    [self.btn9 release];
    [self.btn_xin release];
    [self.btn_jin release];
    [super dealloc];
}


@end



